<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="流域" prop="status">
        <el-input v-model="queryParams.riverBasinName" placeholder="请输入流域关键字"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain 
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete()"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table  :data="tableList" style="width: 100%"  v-if="refreshTable" v-loading="loading" @selection-change="handleSelectionChange">
        <el-table-column  type="selection" width="55" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="basinname" label="流域" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column  prop="updatetime" label="添加日期" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            {{dateFormat(scope.row.updatetime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>

          </template>
        </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form"label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="流域" prop="basinname">
              <el-input v-model="form.basinname" placeholder="请输入流域" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="basinname">
              <el-input v-model="form.sort" placeholder="请输入排序序号" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {basinAdd ,basinEdit,basinRemove,basinList} from '@/api/dataResources/monitorData/riverbasin.js'
export default {
  computed:{
    tableList(){
      if(!this.queryParams.riverBasinName){
        return this.riverBasinList;
      }
      let list = this.riverBasinList.filter(item => item.basinname.indexOf(this.queryParams.riverBasinName)>-1);
      return list
    }
  },
  data(){
    return {
      showSearch: true,
      queryParams:{
        riverBasinName:''
      },
      riverBasinList:[],
      tableSelect:[],
      refreshTable:true,
      loading: false,
      title:'',
      open:false,
      form: {},
    }
  },
  created() {
    this.getList();
  },

  methods: {
    dateFormat(val){
      return this.parseTime(val,'{yyyy}-{mm}-{dd} {hh}:{ii}:{ss}')
    },
    handleQuery() {
      this.getList();
    },
    handleAdd() {
      this.open = true;
      this.title = "添加流域";
      this.form = {basinname:'',sort:''}; 
    },
    handleUpdate(row) {
      this.open = true;
      this.title = "编辑流域";
      this.form = {...row};

    },
    async handleDelete(row = null) {
      let id;
      if(row){
        id = row.id;
      } else if(this.tableSelect.length > 0){
        id = this.tableSelect.map(item => item.id).join(',');
      };
      await basinRemove(id);
      this.getList();
    },
    handleSelectionChange(val){
      this.tableSelect = val;
    },
    handleExport() {

    },
    resetQuery() {
      this.queryParams = {};
      this.getList();
    },
    submitForm(){
      this.$refs["form"].validate((valid) =>{
        if(valid){
          this.saveByApi();
        }
      })
    },      
    async saveByApi(){
      this.open = false;
      let apiFn = this.form.id ? basinEdit : basinAdd;
      await apiFn(this.form);
      await this.getList();
    },
    cancel() {
      this.open = false;
    },
    async getList() {
      this.tableSelect = [];
      this.loading = true
      let res = await basinList();
      if(res.code =='200') {
        this.riverBasinList = res.rows;
      }
      this.loading = false;
    },
  },
};
</script>

<style></style>
